package metartest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import metar.CloudsMetar;
import metar.MetarInfo;
import metar.MetarLogException;
import metar.MetarParseException;
import metar.MetarReader;
import metar.ObservationDateMetar;
import metar.WindMetar;
import model.WeatherModel;

/**
 * Einfaches Frame für MetarReader-Testprogramm
 * @author Michel Metzger, Java-Kurs 2013
 * @version 1.0.0
 *
 */
public class TestWindow extends JFrame {

	private static final long serialVersionUID = -1262230272867005833L;

	private JTextArea textArea; 

	public void showView() {

		setTitle("Test Metar-Info");
		setPreferredSize(new Dimension(600, 400));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		/**
		 * Airport-Code erfassen
		 */
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		panel.setBackground(new Color(153, 204, 204));

		JLabel label = new JLabel("Airport-Code: ");
		panel.add(label);

		String[] someAirportCodes = {
//				"",			// damit die ComboBox am Anfang leer ist
		         "EDDS   (Stuttgart)",
		         "KLOZ   (Lonodon)",
		         "KLGA   (New York)",
		         "EDDM   (München)",
		         "EDDI   (Berlin)",
		         "EDDP   (Leipzig)",
		         "LFPB   (Paris)",
		         "EHAM   (Amsterdam)",
		         "UUEE   (Moskau)",
		         "LEGT   (Madrid)",
		         "LIRA   (Rom)",
		         "LGAV   (Athen)"
		};

		final JComboBox<String> locationBox = new JComboBox<String>(someAirportCodes);
		locationBox.setEditable(true);
		panel.add(locationBox);
		
		/**
		 * mit GO die Abfrage durchführen
		 */
		JButton goButton = new JButton(" Go ");
		goButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
	        	goAction(locationBox.getSelectedItem().toString());
			}
		});
		panel.add(goButton);

		add(panel);

		/**
		 * TextArea zur Anzeige der Wetterinfos
		 */
		textArea = new JTextArea();
		textArea.setBackground(new Color(255, 255, 102));
		textArea.setFont(new Font("Monospaced", Font.ITALIC, 14));
		textArea.setEditable(false);
		
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setPreferredSize(new Dimension(550, 350));
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);		
		add(scrollPane);
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(new FlowLayout());
		panel2.setBackground(new Color(204, 179, 153));
		
		/**
		 * Exit zum Beenden des Programms
		 */
		JButton exitButton = new JButton(" Exit ");
		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		panel2.add(exitButton);
		add(panel2);
		
		pack();

		/**
		 * Fenster auf dem Bildschirm zentrieren
		 */
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	    setLocation((int) ((d.getWidth() - getWidth()) / 2), (int) ((d.getHeight() - getHeight()) / 2));
		
	    /**
	     * und anzeigen
	     */
		setVisible(true);
	}

	/**
	 * Daten für den angegebenen Flughafen abrufen und ausgeben
	 * @param location
	 */
	public void goAction(String location) {
		if (location != null && !location.isEmpty()) {
			location = location.substring(0, 4);
			try {
				WeatherModel wm = new MetarReader(location).getMetarInfo();
				if (wm != null) {
					ObservationDateMetar oDate = new ObservationDateMetar();
					if (wm.getObservationTime() != null && !wm.getObservationTime().isEmpty()) {
						oDate.parseParam(wm.getObservationTime());
					}
					String windInfo = ""; 
					WindMetar wind = new WindMetar();
					if (wm.getWindDirectionInfo() != null && !wm.getWindDirectionInfo().isEmpty()) {
						wind.parseParam(wm.getWindDirectionInfo());
						windInfo = " ==> " + WindMetar.windDirectionAsString(wind.getDirection());
						if (wind.getSpeed() > 0) {
							windInfo += " , " + wind.getSpeed() + "kt"; 
						}
						if (wind.getSpeedGusts() > 0) {
							windInfo += "   wind gusts " + wind.getSpeedGusts() + "kt"; 
						}
					}
					String cloudInfo = "";
					CloudsMetar clouds = new CloudsMetar();
					if (wm.getCloudiness() != null && !wm.getCloudiness().isEmpty()) {
						clouds.parseParam(wm.getCloudiness());
						cloudInfo = " ==> " + clouds.getClouds();
						if (clouds.getCloudCeiling() > 0) {
							cloudInfo += " " + clouds.getCloudCeiling() + "ft";
						}
					}
					textArea.setText("Airport:            " + location + "\n\n");
					textArea.append("info=               " + wm.getInfo() + "\n");
					textArea.append("observationTime=    " + wm.getObservationTime() + " ==> " + oDate.getDate() + "\n");
					textArea.append("timestamp=          " + wm.getTimeStamp() + "\n");
					textArea.append("date=               " + wm.getDate() + "\n");
					textArea.append("temperature=        " + wm.getTemperature() + "°C\n");
					textArea.append("airPressure=        " + wm.getAirPressure() + "hPa\n");
					textArea.append("windDirectionInfo=  " + wm.getWindDirectionInfo() + windInfo + "\n");
					textArea.append("cloudiness=         " + wm.getCloudiness() + cloudInfo + "\n");
					textArea.append("visibility=         " + wm.getVisibility() + "m\n");
				}
			} catch (MetarLogException e) {
				if (MetarInfo.isDebugMode()) {
					e.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, e.getMessage(), "Error while writing logfile", JOptionPane.ERROR_MESSAGE);
			} catch (MetarParseException e) {
				if (MetarInfo.isDebugMode()) {
					e.printStackTrace();
				}
				JOptionPane.showMessageDialog(null, e.getMessage(), "Error while parsing metar info", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
}
